﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientLibrary;
using System.IO;

namespace DocumentDigitalization
{
    public partial class UserControl_exportpdf : UserControl
    {
        Form form;
        SystemState Systemstate;

        public UserControl_exportpdf()
        {
            InitializeComponent();
        }

        public UserControl_exportpdf(Form form, SystemState systat)
        {
            InitializeComponent();
            this.form = form;
            this.Systemstate = systat;
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
            }
            this.Systemstate.DeleteTemporaryImageFiles();
            this.Visible = false;
            this.Enabled = false;
            UserControl_MainPage uc = new UserControl_MainPage(this.form, this.Systemstate);
            this.form.Controls.Add(uc);            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            DialogResult result = openFileDialog.ShowDialog(); // Show the dialog.

            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog.FileName; //path+filename
                string[] files = this.Systemstate.ReadPDF(file);
                for (int i = 0; i < files.Length; i++)
                {
                    this.listView_images_files.Items.Add(files[i]);
                }
                this.Text = "Document Digitalizer Standalone - Current batch:" + this.Systemstate.LastBatch.ToString();
                MessageBox.Show("Image files are created.", "Success");                
            }
            
        }

        private void listView_images_files_MouseClick(object sender, MouseEventArgs e)
        {
            ListView lv = (ListView)sender;
            if (lv.SelectedItems.Count == 1)
            {
                ListViewItem item = lv.SelectedItems[0];                
                pictureBox1.Image = System.Drawing.Image.FromFile(item.Text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.comboBox_template.Items.Clear();
            List<Template> templates = this.Systemstate.getTemplates();
            for (int i = 0; i < templates.Count; i++)
            {
                this.comboBox_template.Items.Add(templates[i].Name);
            }
        }

        private void comboBox_template_SelectedIndexChanged(object sender, EventArgs e)
        {
            string template_name = this.comboBox_template.SelectedItem.ToString();
            for (int i = 0; i < this.Systemstate.getTemplates().Count; i++)
            {
                if (this.Systemstate.getTemplates()[i].Name.Equals(template_name))
                {
                    userControl_TemplateBox.setSystemState(this.Systemstate);
                    userControl_TemplateBox.setTemplate(this.Systemstate.getTemplates()[i]);
                    break;
                }
            }
        }
    }
}
